<?php
/**
 * 文件系统缓存
 * @author clarepan <clarepangz@gmail.com>
 *
 */
class Cache_File
{
    const CACHE_DIR_NUM = 500;
    const DEFAULT_EXPIRE = 300;
    var $cache_path='';
    
    public function __construct($path='')
    {
        $this->cache_path = _CACHE_;
        $path && $this->cache_path = $path;
        $this->cache_path = realpath($this->cache_path);
        if(!$this->cache_path) return false;
        return true;
    }
    
    /**
     * 
     * @param unknown $key
     * @param unknown $value
     * @param number $expire 过期时间,单位：秒
     * @return boolean|number
     */
    public function set($key,$value,$expire=0)
    {
        if (!$key)
        {
            return false;
        }
        $expire || $expire = self::DEFAULT_EXPIRE;
        $cache_file = $this->_get_cache_path($key);
        $cache_data = "<?php\r\n/**\r\n *  @Created By LzZen FileCacheServer\r\n *  @Time:" . date('Y-m-d H:i:s') . "\r\n */";
        $cache_data .= $this->_get_expire_condition(intval($expire));
        $cache_data .= "\r\nreturn " . var_export($value, true) .  ";\r\n";
        $cache_data .= "\r\n?>";
        
        return file_put_contents($cache_file, $cache_data, LOCK_EX);
    }
    
    public function get($key)
    {
        $cache_file = $this->_get_cache_path($key);
        if (!is_file($cache_file))
        {
            return false;
        }
        $data = include($cache_file);
        return $data;
    }
    
    public function disset($key)
    {
        $cache_file = $this->_get_cache_path($key);
        return @unlink($cache_file);
    }
    
    function clear()
    {
        $dir = opendir($this->cache_path);
        while (false !== ($item = readdir($dir)))
        {
            if ($item == '.' || $item == '..' || substr($item, 0, 1) == '.')
            {
                continue;
            }
            $item_path = $this->cache_path . '/' . $item;
            if (is_dir($item_path))
            {
                @lzzen_rmdir($item_path);
            }
            else
            {
                @unlink($item_path);
            }
        }
    
        return true;
    }

    
    /**
     * 
     * @param string $path 绝对路径
     */
    function set_cache_dir($path)
    {
        $this->cache_path = $path;
    }
    
    private function _get_expire_condition($ttl = 0)
    {
        if (!$ttl)
        {
            return '';
        }
    
        return "\r\n\r\n" . 'if(filemtime(__FILE__) + ' . $ttl . ' < time())return false;' . "\r\n";
    }
    
    private function _get_cache_path($key)
    {
        $dir = str_pad(abs(crc32($key)) % self::CACHE_DIR_NUM, 4, '0', STR_PAD_LEFT);
        !is_dir($this->cache_path . '/' . $dir) && mkdir($this->cache_path . '/' . $dir);
        return $this->cache_path . '/' . $dir .  '/' . $this->_get_file_name($key);
    }
    
    private function _get_file_name($key)
    {
        return md5($key) . '.cache.php';
    }
    
}